package com.market.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.bean.CommonParam;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.IAddressInfoService;
import com.block.common.service.IChannelCommonService;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.*;
import com.block.db.mapper.MemberBaseInfoMapper;
import com.block.db.mapper.MemberSourceInfoMapper;
import com.market.member.model.param.UserSourceParam;
import com.market.member.model.vo.UserSourceVO;
import com.market.member.service.IMemberSourceInfoService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * <p>
 * 用户资质信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-11
 */
@Service
public class MemberSourceInfoServiceImpl extends ServiceImpl<MemberSourceInfoMapper, MemberSourceInfo> implements IMemberSourceInfoService {

    private static final Logger log = LogFactory.getCommLog();

    @Autowired
    IAddressInfoService addressInfoService;

    @Resource
    MemberBaseInfoMapper memberBaseInfoMapper;
    @Autowired
    IChannelCommonService channelCommonService;

    @Override
    public ResponseVo updateUserSource(UserSourceParam userSourceParam, CommonParam commonParam, Integer userType){

        try {

            // 查询用户实名信息
//            MemberAuthInfo memberAuthInfo = memberAuthInfoService.getById(userSourceParam.getUid());
//            if (ObjectUtil.isEmpty(memberAuthInfo)){
//                return new ResponseVo(AppRspCodeConstant.ACTION_ERROR, "未完成实名认证");
//            }
//            if (!ObjectUtil.equals(memberAuthInfo.getAuthStatus(), AuthStatusEnum.SUCCESS.getValue())){
//                return new ResponseVo(AppRspCodeConstant.ACTION_ERROR, "实名认证未通过，请重试");
//            }

            // 更新用户信息
            UserSourceVO userSourceVO = null;//this.saveUserSourceInfo(memberAuthInfo, userSourceParam, commonParam);

            boolean success = userSourceVO.isSuccess();
            if (!success){
                return new ResponseVo(AppRspCodeConstant.ACTION_ERROR, "更新资质信息失败");
            }

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(userSourceVO));

            // 新注册用户
            return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION,"更新资质信息失败");
    }

    private UserSourceVO saveUserSourceInfo( UserSourceParam userSourceParam, CommonParam commonParam){

        /**
         * 查询
         */
        boolean isNew = false;
        MemberSourceInfo memberSourceInfo = null;//this.baseMapper.getEntityByUidLock(memberAuthInfo.getUid());
        MemberBaseInfo memberBaseInfo = null;//memberBaseInfoMapper.getMemberInfoByUidLock(memberAuthInfo.getUid());

        MemberSourceInfo newEntity = new MemberSourceInfo();
//        newEntity.setUid(memberAuthInfo.getUid());
        if (ObjectUtil.isEmpty(memberSourceInfo)){
            isNew = true;
            newEntity.setDateInfo(LocalDate.now().toString());
            newEntity.setReqIp(commonParam.getReqIp());

            ApplicationInfo applicationInfo = commonParam.getApplicationInfo();
            ChannelBaseInfo channelBaseInfo = commonParam.getChannelBaseInfo();

            newEntity.setAppCode(applicationInfo.getAppCode());
            newEntity.setAppName(applicationInfo.getAppName());

            newEntity.setChannelCode(channelBaseInfo.getChannelCode());
            newEntity.setChannelName(channelBaseInfo.getChannelName());

//            newEntity.setRealname(memberAuthInfo.getRealname());
//            newEntity.setIdcard(memberAuthInfo.getIdcard());
            newEntity.setMobile(memberBaseInfo.getMobile());
            newEntity.setMobileMd5(memberBaseInfo.getMobileMd5());

//            Integer sex = IdcardUtil.getGenderByIdCard(memberAuthInfo.getIdcard())==1?1:2;
//            Integer age = IdcardUtil.getAgeByIdCard(memberAuthInfo.getIdcard());
//            newEntity.setAge(age);
//            newEntity.setSex(sex);
        }

        String cityCode = userSourceParam.getCityCode();
        if (StrUtil.length(cityCode) != 6){
            cityCode = StrUtil.fillAfter(cityCode, '0', 6);
        }

        AddressInfo addressInfo = addressInfoService.getById(cityCode);
        if (ObjectUtil.isNotEmpty(addressInfo)){
            newEntity.setCity(addressInfo.getCity());
            newEntity.setCityCode(addressInfo.getCityCode());
            newEntity.setProvince(addressInfo.getProvince());
            newEntity.setProvinceCode(addressInfo.getProvinceCode());
        }else{
            // TODO: 2023/8/24 消息通知

        }

        newEntity.setLoanAmount(userSourceParam.getLoanAmount());
        newEntity.setLoanTime(userSourceParam.getLoanTime());
        newEntity.setHouse(userSourceParam.getHouse());
        newEntity.setCar(userSourceParam.getCar());
        newEntity.setGjj(userSourceParam.getGjj());
        newEntity.setShebao(userSourceParam.getShebao());
        newEntity.setBaoxian(userSourceParam.getBaoxian());
        newEntity.setZhima(userSourceParam.getZhima());
        newEntity.setHuabei(userSourceParam.getHuabei());
        newEntity.setBaitiao(userSourceParam.getBaitiao());
        newEntity.setWeili(userSourceParam.getWeili());
        newEntity.setShouru(userSourceParam.getShouru());
        newEntity.setXueli(userSourceParam.getXueli());
        newEntity.setYongtu(userSourceParam.getYongtu());
        newEntity.setYuqi(userSourceParam.getYuqi());
        newEntity.setXinyongka(userSourceParam.getXinyongka());
        newEntity.setGongzuo(userSourceParam.getGongzuo());
        boolean saveRes = false;
        if (isNew){
            saveRes = this.save(newEntity);
            if (saveRes){
                MemberBaseInfo updateBaseInfo = new MemberBaseInfo();
//                updateBaseInfo.setUid(memberAuthInfo.getUid());
                updateBaseInfo.setSourceFlag(1);
                memberBaseInfoMapper.updateById(updateBaseInfo);
            }
        }else{
            saveRes = this.updateById(newEntity);
        }
        UserSourceVO userSourceVO = new UserSourceVO();
        userSourceVO.setSuccess(saveRes);
        userSourceVO.setNew(isNew);

        return userSourceVO;
    }


}

